<template>
	<view>
		<view class="bj">
			<u-navbar title="汽车保养" :autoBack="true" bgColor="#3284fc"
				titleStyle="font-size:18px; color:white;height:44px;line-height:44px" leftIconColor="#fff">
			</u-navbar>
			<u-cell-group :border="false"
				:customStyle="{backgroundColor:'#ffffff',borderRadius:'10px',margin:'10px auto',width:'350px',}">
				<u-cell>
					<template v-slot:title>
						<span style="font-size: 16px;font-weight: 600;">服务评价</span>
					</template>
				</u-cell>
				<view class="pj">
					<view v-if="order.shop!= null">
						<image :src="order.shop.shopImages[0].photo"></image>
					</view>
					<view class="pf">
						<view style="margin-bottom: 10px;">
							<span v-if="order.shop != null">
								{{order.shop.storeName}}
							</span>
						</view>
						<u-rate v-model="evaluate.star" count="5" size="22px"></u-rate>
					</view>
				</view>
			</u-cell-group>

			<u-cell-group :border="false"
				:customStyle="{backgroundColor:'#ffffff',borderRadius:'10px',margin:'10px auto',width:'350px',}">
				<u-cell>
					<template v-slot:title>
						<span style="font-size: 16px;font-weight: 600;">评语</span>
					</template>
				</u-cell>
				<u--textarea height="100" v-model="evaluate.comments" placeholder="请留下您宝贵的意见和建议"
					style="margin: 20px;"></u--textarea>
			</u-cell-group>
			<u-cell-group :border="false"
				:customStyle="{backgroundColor:'#ffffff',borderRadius:'10px',margin:'10px auto',width:'350px',}">
				<u-cell>
					<template v-slot:title>
						<span style="font-size: 16px;font-weight: 600;">照片</span>
					</template>
				</u-cell>
				<u-upload v-model="evaluate.imgList" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"
					name="1" multiple :maxCount="10" style="margin: 20px;"></u-upload>
			</u-cell-group>
			<u-button @click="tj(order)" text="提交"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				order: '',
				evaluate: {
					userId: '',
					orderId: '',
					star: 0,
					imgList: '',
					comments: ''
				}
			}
		},
		onLoad(e) {
			this.evaluate.userId = e.userId;
			this.evaluate.orderId = e.orderId;
			this.getOrder();
		},
		methods: {
			tj(order) {
				let that = this
				this.$http({
					url: 'api/order/evaluate',
					method: 'POST',
					data: that.evaluate,
					success(res) {
						order.state = 5;
						that.xg(order);
						uni.navigateTo({
							url:'/pages/my/wddd/wddd'
						})
					}
				})
			},
			xg(qq) {
				this.$http({
					url: 'api/order/order',
					method: 'PUT',
					data: qq,
					success(res) {
						console.log(res)
					}
				})
			},
			getOrder() {
				let that = this;
				this.$http({
					url: 'api/order/order/' + this.evaluate.orderId,
					method: 'GET',
					success(res) {
						console.log(res)
						that.order = res.data.data

					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})

			}
		}
	}
</script>

<style scoped>
	.u-button {
		width: 260px;
		background-color: #0662e7;
		color: #fff;
		border-radius: 20px;
		margin-top: 20px;
	}

	.pf {
		margin-left: 10px;
	}

	.pj {
		display: flex;
		padding: 20px;

	}

	.pj image {
		width: 120px;
		height: 70px;
		border-radius: 10px;
	}

	.bj {
		width: 100%;
		height: 150px;
		background-color: #3384fc;
		margin-top: 44px;
	}
</style>